//
//  BaseController.m
//  Kugou-OC
//
//  Created by BH on 16/4/5.
//  Copyright © 2016年 Sunny. All rights reserved.
//

#import "BaseController.h"
#import "GlobalDefine.h"
#import "PlaylistView.h"
#import "PlayController.h"
#import "MusicInfoModel.h"
#import "NSTimer+Control.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import <SDAutoLayout/UIView+SDAutoLayout.h>

@interface BaseController ()<BottomPlayViewDelegate>{
    

}

@end

@implementation BaseController
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置statusbar颜色
    [self setNeedsStatusBarAppearanceUpdate];
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    //添加底部工具栏
    _bottomPlay = [BottomPlayView shareBottomView];
    _bottomPlay.delegate = self;
    [window addSubview:_bottomPlay];
    _bottomPlay.sd_layout
    .leftSpaceToView(window,0)
    .rightSpaceToView(window,0)
    .bottomSpaceToView(window,0)
    .heightIs(90);
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    [_bottomPlay addGestureRecognizer:tap];
}

//单击bottomview显示播放界面
-(void)handleTap:(id)sender{
    PlayController *playCtrl = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"PlayController"];
    playCtrl.singsList = _bottomPlay.musicList;
    playCtrl.playerItem = _bottomPlay.currentPlayer;
    playCtrl.currentIndex = _bottomPlay.currentPlayIndex;
    [self.navigationController pushViewController:playCtrl animated:YES];
}

-(void)setBottomHidden:(BOOL)bottomHidden
{
    _bottomHidden = bottomHidden;
    _bottomPlay.hidden = bottomHidden;
}

#pragma mark - BottomPlayView delegate
-(void)showPlayListViewWithItem:(AVPlayerItem *)item array:(NSArray *)array index:(NSInteger)index hidden:(BOOL)hidden{

    PlaylistView *playListV = [[PlaylistView alloc] init];
    playListV.playList = array;
    playListV.currentIndex = index;
    playListV.currentItem = item;
    playListV.Play = ^(NSArray *playList,NSInteger playIndex){
        _bottomPlay.musicList = playList;
        [_bottomPlay setupPlayerWithModel:playList[playIndex]];
    };
    
    if (array.count<5) {
        playListV.frame = CGRectMake(0, SCREEN_HEIGHT-70, SCREEN_WIDTH, array.count*60+44);
    }else{
        playListV.frame = CGRectMake(0, SCREEN_HEIGHT-70, SCREEN_WIDTH, 5*60+44);
    }
    
    if (!hidden) {
        [self.view addSubview:playListV];
        //显示
        [UIView animateWithDuration:0.5 animations:^{
            [self.view bringSubviewToFront:_bottomPlay];
            playListV.transform = CGAffineTransformMakeTranslation(0, -playListV.height);
        }];
        
    }else{
        
        for (UIView *view in self.view.subviews) {
            if ([view isKindOfClass:[PlaylistView class]]) {
                //隐藏
                [UIView animateWithDuration:0.5 animations:^{
                    view.transform = CGAffineTransformIdentity;
                    
                } completion:^(BOOL finished) {
                    [view removeFromSuperview];
                }];
            }
        }
    }
}

@end
